package api

import (
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"github.com/gin-gonic/gin"
)

// 统一评价（同时支持司机评价乘客和乘客评价司机）
func Rating(c *gin.Context) {
	var req request.Rating
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.Rating(c, &__.RatingReq{
		OrderId:    req.OrderId,
		RaterId:    req.RaterId,
		TargetId:   req.TargetId,
		RatingType: req.RatingType,
		Score:      req.Score,
		Comment:    req.Comment,
		Tags:       req.Tags,
		Images:     req.Images,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "评价成功", res)
}

// 获取用户收到的评价列表
func GetRatingsByTargetId(c *gin.Context) {
	var req request.GetRatingsByTargetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.GetRatingsByTargetId(c, &__.GetRatingsByTargetIdReq{
		TargetId:   req.TargetId,
		RatingType: req.RatingType,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取用户收到的评价列表成功", res)
}

// 获取用户平均评分
func GetAverageRating(c *gin.Context) {
	var req request.GetAverageRating
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.GetAverageRating(c, &__.GetAverageRatingReq{
		TargetId:   req.TargetId,
		RatingType: req.RatingType,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取用户平均评分成功", res)
}

// 获取指定订单的评价
func GetRatingByOrder(c *gin.Context) {
	var req request.GetRatingByOrder
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.GetRatingByOrder(c, &__.GetRatingByOrderReq{
		OrderId:    req.OrderId,
		RatingType: req.RatingType,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取指定订单的评价成功", res)
}

// 紧急求助
func EmergencyHelp(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.EmergencyHelp
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.EmergencyHelp(c, &__.EmergencyHelpReq{
		OrderId:     req.OrderId,
		UserId:      uint32(userId),
		Location:    req.Location,
		Description: req.Description,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "紧急求助成功", res)
}

// 行程分享
func ShareTrip(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.ShareTrip
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 调用gRPC服务
	res, err := config.OrderClient.ShareTrip(c, &__.ShareTripReq{
		OrderId:   req.OrderId,
		UserId:    uint32(userId),
		ShareTo:   req.ShareTo,
		ShareType: req.ShareType,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "行程分享成功", res)
}

// 获取保险信息
func GetInsuranceInfo(c *gin.Context) {
	var req request.GetInsuranceInfo
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.GetInsuranceInfo(c, &__.GetInsuranceInfoReq{
		OrderId: req.OrderId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取保险信息成功", res)
}

// 获取用户预约订单列表
func GetUserScheduledOrders(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.GetUserScheduledOrders
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.GetUserScheduledOrders(c, &__.GetUserScheduledOrdersReq{
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "获取用户预约订单列表成功", res)
}

// 取消预约订单
func CancelScheduledOrder(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.CancelScheduledOrder
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.CancelScheduledOrder(c, &__.CancelScheduledOrderReq{
		OrderId: req.OrderId,
		UserId:  uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "取消预约订单成功", res)
}
